﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.Xml;

using umbraco;
using umbraco.blog4.spam;
using umbraco.BusinessLogic;

namespace umbraco.blog4 {
    public class Config {
        public static SpamChecker GetChecker() {
            SpamChecker checker = null;
            try {
                string assemblyFile = HttpContext.Current.Server.MapPath(String.Format("{0}/..{1}.dll", GlobalSettings.Path, GetProviderAssembly()));
                Assembly checkerAssembly = Assembly.LoadFrom(assemblyFile);
                checker = (SpamChecker)Activator.CreateInstance(checkerAssembly.GetType(GetProviderType()));
            } catch (Exception ex) {
                Log.Add(LogTypes.Error, -1, String.Format("Could not load spam checker: {0}", ex.ToString()));
            }
            return checker;
        }

        public static string GetProviderAssembly() {
            XmlDocument config = new XmlDocument();
            config.Load(HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/plugins/blog4umbraco/SpamChecker.config"));
            XmlNode key = config.SelectSingleNode("/SpamChecker");
            return key.Attributes["assembly"].Value;
        }

        public static string GetProviderType() {
            XmlDocument config = new XmlDocument();
            config.Load(HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/plugins/blog4umbraco/SpamChecker.config"));
            XmlNode key = config.SelectSingleNode("/SpamChecker");
            return key.Attributes["type"].Value;
        }
    }
}
